package aip.assignment2;

import aip.assignment2.webservice.Purchase;
import aip.assignment2.webservice.SupplierService;
import aip.assignment2.webservice.UpdatePurchaseException_Exception;
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceRef;

/**
 *
 * @author Stone
 */
@ManagedBean(name = "supplier")
@ViewScoped
public class Supplier
{
	private String passwordBackup;
	public void setPasswordBackup(String passwordBackup)
	{
		this.passwordBackup = passwordBackup;
	}
	public String getPasswordBackup()
	{
		return passwordBackup;
	}
	
	private String userName;
	/**
	 * @return the userName
	 */
	public String getUserName()
	{
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName)
	{
		this.userName = userName;
	}
	private String password;
	/**
	 * @return the password
	 */
	public String getPassword()
	{
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password)
	{
		this.password = password;
	}
	private List<Purchase> currentPurchases;
	/**
	 * @return the currentPurchases
	 */
	public List<Purchase> getCurrentPurchases()
	{
		return currentPurchases;
	}
	/**
	 * @param currentPurchases the currentPurchases to set
	 */
	public void setCurrentPurchases(List<Purchase> currentPurchases)
	{
		this.currentPurchases = currentPurchases;
	}
	
	/**
	 * This method adds a certain message to the current JSF context.
	 * @param severity the severity of the message
	 * @param summary the message summary
	 * @param detail the message detail
	 */
	public void addMessage(FacesMessage.Severity severity, String summary)
	{
		if (severity != null && summary != null)
		{
			FacesMessage successMessage = new FacesMessage(severity, summary, "");
			FacesContext.getCurrentInstance().addMessage(null, successMessage);
		}
	}
	
	@WebServiceRef()
	private SupplierService m_SupplierService;
	public void search()
	{
		passwordBackup=password;//backup the current password since the current password will be reset
		aip.assignment2.webservice.Supplier supplier = m_SupplierService.getSupplierPort();
		Map<String, Object> requestContext = ((BindingProvider) supplier).getRequestContext();
		requestContext.put(BindingProvider.USERNAME_PROPERTY, userName);
		requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
		try
		{
			currentPurchases = supplier.list();
		}
		catch (Exception ex)
		{
			addMessage(FacesMessage.SEVERITY_ERROR, "Search Authentication Failure.");
		}
		finally
		{
		
			try
			{
				((Closeable) supplier).close();
			}
			catch (IOException ex)
			{
				Logger.getLogger(Supplier.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	
	}
	/**
	 * This method sends an existing purchase.
	 * @param purchase the purchase to be sent.
	 */
	public void send(Purchase purchase)
	{
		if (purchase == null)
		{
			return;
		}
		aip.assignment2.webservice.Supplier supplier = m_SupplierService.getSupplierPort();
		Map<String, Object> requestContext = ((BindingProvider) supplier).getRequestContext();
		requestContext.put(BindingProvider.USERNAME_PROPERTY, userName);
		//requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
		requestContext.put(BindingProvider.PASSWORD_PROPERTY, passwordBackup);
		//restore the old password back to the current password
		if (!passwordBackup.isEmpty())
			password=passwordBackup;
		try
		{
			supplier.update(purchase.getNumber(), "SENT");
			addMessage(FacesMessage.SEVERITY_INFO, String.format("The purchase (%s) is sent.", purchase.getNumber()));
		}
		catch (UpdatePurchaseException_Exception ex)
		{
			addMessage(FacesMessage.SEVERITY_ERROR, "Unable to send the purchase.");
		}
		catch (Exception ex)
		{
			addMessage(FacesMessage.SEVERITY_ERROR, "Update status Authentication Failure.");
		}
		finally
		{
			try
			{
				((Closeable) supplier).close();
			}
			catch (IOException ex)
			{
				Logger.getLogger(Supplier.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		search();//so that we can fetch the result from the dataase again.
	}
}
